import demandApi from "@/static/js/enterpriseServer/demand.js"
export default {
	data() {
		return {
			demandList: [],
			//分页参数
			pageShow: false,
			page: 1,
			pageSize: 10,
			isRefreshing: false,
			listStatus: 'nomore'
		}
	},
	onLoad() {

	},
	onShow() {
		this.getList()
	},
	methods: {
		async getList() {
			this.listStatus = 'loading'
			uni.showLoading({
				title: '加载中'
			})
			let obj = {
				page: this.page,
				pagesize: this.pageSize
			}
			let res = await demandApi.getMyDemand(obj)
			if (res.code === 1) {
				let arr = []
				if (this.page == 1) {
					this.demandList = []
				}
				let newList = res.data
				if (newList.length >= this.pageSize) {
					this.listStatus = 'more'
				} else {
					this.listStatus = 'nomore'
				}
				let arr1 = arr.concat(newList)
				arr1.forEach(val => {
					let obj = {
						id: val.id,
						name: val.name,
						time: val.createtime_text,
						city_name: val.city_name,
						status: val.status
					}
					this.demandList.push(obj)
				})
				this.pageShow = true
				this.isRefreshing = false
				uni.hideLoading()
			}
		},

		//跳转需求详情
		goDemandDetail(val) {
			uni.navigateTo({
				url: '/enterpriseServer/demandDetails/index?id=' + val.id
			})
		},


		//关闭或删除需求
		editDemand(val) {
			let that = this
			if (val.status === 0) {
				uni.showModal({
					cancelColor: "#999",
					cancelText: '再想想',
					confirmColor: "#0177fd",
					confirmText: '确认关闭',
					content: '确认关闭此需求吗？',
					success: function (ots) {
						if (ots.confirm) {
							demandApi.closeDemand({ id: val.id }).then(res => {
								if (res.code === 1) {
									that.$tools.msg('关闭成功')
									that.getList()
								} else {
									that.$tools.msg(res.msg)
								}
							})
						}
					}
				})
			} else {
				demandApi.delDemand({ id: val.id }).then(res => {
					if (res.code === 1) {
						that.$tools.msg('删除成功')
						that.getList()
					} else {
						that.$tools.msg(res.msg)
					}
				})
			}
		},

		//下拉刷新
		refresherrefresh() {
			this.isRefreshing = true;
			this.page = 1
			this.getList()
		},

		//上拉加载
		loadMore() {
			if (this.listStatus == 'more') {
				this.page += 1
				this.getList()
			}
		}
	}
}